package com.hm.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hm.model.DaysEntity;
import com.hm.model.MandatoryEntity;
import com.hm.services.ChmService;

@Controller
public class ViewController {
	
	@Autowired
	private ChmService controllerService;
	
	public ChmService getControllerService() {
		return controllerService;
	}

	public void setControllerService(ChmService controllerService) {
		this.controllerService = controllerService;
	}
	
	
	@RequestMapping(value ="/view", method = RequestMethod.GET)
	 public String getViewPage(ModelMap model, HttpSession session) {

		List<DaysEntity> days = new ArrayList<DaysEntity>();
		days = controllerService.getApprovedHols();
		
		List<MandatoryEntity> mandatory = new ArrayList<MandatoryEntity>();
		mandatory = controllerService.getMandatoryDays();
		
		 model.addAttribute("mandatory", mandatory);
		 model.addAttribute("days", days);
				 return "view";
	    }
	
	
	@RequestMapping(value ="/showmydays", method = RequestMethod.GET)
	 public String getReqPage(ModelMap model, HttpSession session) {

		List<MandatoryEntity> mandatory = new ArrayList<MandatoryEntity>();
		mandatory = controllerService.getMandatoryDays();
		for (MandatoryEntity d : mandatory) {
			System.out.println("Date : " + d.getDateCreated() + " Day off: "
					+ d.getMandatoryDay());
		}
		
		List<DaysEntity> days = new ArrayList<DaysEntity>();
		days = controllerService.getApprovedHols();
		
		
		List<Date> finalDates = new ArrayList<Date>();

		for(DaysEntity d : days){
			
		Date start = d.getStartDate(); //call dates from the database
		Date end = d.getEndDate();
	
		
		long interval = 24*1000 * 60 * 60; // 1 day in millis
		long endTime =end.getTime() ; // create your endtime is the last holiday the booker will take
		long curTime = start.getTime();
		while (curTime <= endTime) {
		    finalDates.add(new Date(curTime));//add all dates between the two dates inc. the dates themselves
		    curTime += interval;
		}
		
		}	
	
		
		model.addAttribute("man", mandatory);		
		 model.addAttribute("days", days);//passes all details of the holiday booking
		 model.addAttribute("day", finalDates);//passes all dates that will be displayed so that dates between startDate and endDate will be shown
				 return "view";
	    }
}